package com.jy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jy.entity.Area;
import com.jy.mapper.AreaMapper;
import com.jy.service.IAreaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * <p>
 * 车位区域表 服务实现类
 * </p>
 *
 * @author xiexingxin
 * @since 2022-11-15
 */
@Service
@Slf4j
public class AreaServiceImpl extends ServiceImpl<AreaMapper, Area> implements IAreaService {

    @Autowired
    private AreaMapper areaMapper;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    boolean debug = log.isDebugEnabled();
    
    @Override
    public List<Area> getAllInfo() {


        ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue();
        // 拿到Redis中的Key为areaAllInfo的Values
        List<Area> areas = (List<Area>) valueOperations.get("areaAllInfo");
        // 如果areas为空 则取mysql数据库查询并且添加到Redis Key为areaAllInfo
        if (CollectionUtils.isEmpty(areas)) {
            LambdaQueryWrapper<Area> lambdaQueryWrapper = Wrappers.lambdaQuery();
            List<Area> areaList = areaMapper.selectList(lambdaQueryWrapper);
            if (debug){
                log.debug("开始从数据库中获取数据：{}",areaList);
            }
            log.info("从数据库中获取到数据：{}",areaList);
            valueOperations.set("areaAllInfo", areaList);
        }

        return areas;
    }
}
